<!DOCTYPE html>
<html> 
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>yukari world</title>
    <#include "/lib/base_source.ftl">
      <style type="text/css">
      </style>
  </head> 
  <body>
    <div class='whole-container'>
      <#include "/lib/header.ftl">
        <div>
          <ul class="breadcrumb">
            <li>抽奖管理</li><span class="divider">/</span>
            <li>弹幕抽奖</li>
          </ul>
        </div>
        <div class="container-fluid">
          <div class="row-fluid">
            <div class="container span12">
              <#include "/lib/alert.ftl">
                 <div class='main-content'>
                    <div class="well form-horizontal">
                        <div class='main-action'>
                        	<form id="findForm">
                        		<div class="control-group">
	                                <label class="control-label">问题</label>
	                                <div class="controls">
	                                    <input type="text" id="problem" name="problem" value="${status.problem!}" style="width:300px" <#if (status.drawTicketFlag || status.endTime??)  >readonly="readonly"</#if> />
	                                </div>
	                            </div>
                        		<#if status.drawTicketFlag>
                        		<#list status.keySet as keys>
                        		<div class="control-group">
	                                <label class="control-label">关键字${keys_index+1}</label>
	                                <div class="controls">
	                                	<input type="text" name="key" value="<#list keys as key>${key}<#if key_has_next>,</#if></#list>" style="width:200px" readonly="readonly" />
	                                </div>
	                            </div>
                        		</#list>
	                            <div class="control-group">
	                                <label class="control-label">开始时间</label>
	                                <div class="controls">
	                                    ${status.startTime}
	                                </div>
	                            </div>
	                            <div class="control-group" id="lastDiv">
	                                <label class="control-label"></label>
	                                <div class="controls">
	                                    <a id="submitBtn" class="btn btn-info btn-sm" disabled="disabled">竞猜中...</a>
	                                </div>
	                            </div>
	                            <#elseif status.endTime??>
	                            <#list status.keySet as keys>
                        		<div class="control-group">
	                                <label class="control-label">关键字${keys_index+1}</label>
	                                <div class="controls">
	                                	<input type="text" name="key" value="<#list keys as key>${key}<#if key_has_next>,</#if></#list>" style="width:200px" readonly="readonly" />
	                                </div>
	                            </div>
                        		</#list>
	                            <div class="control-group">
	                                <label class="control-label">开始时间</label>
	                                <div class="controls">
	                                    ${status.startTime}
	                                </div>
	                            </div>
	                            <div class="control-group">
	                                <label class="control-label">结束时间</label>
	                                <div class="controls">
	                                    ${status.endTime}
	                                </div>
	                            </div>
	                            <div class="control-group">
	                                <label class="control-label">中奖人</label>
	                                <div class="controls">
                                    	<div><span>Lv.${status.auth.lv}&nbsp;${status.auth.name}&nbsp;<#if (status.auth.fansLv > 0)><img src="${rc.contextPath}/images/chuizi2.png" />+${status.auth.fansLv}</#if></span></div>
	                                </div>
	                            </div>
	                            <div class="control-group" id="lastDiv">
	                                <label class="control-label"></label>
	                                <div class="controls">
	                                    <a id="submitBtn" onclick="clearStatus()" class="btn btn-info btn-sm">清除</a>
	                                </div>
	                            </div>
	                            <#else>
	                            <div class="control-group">
	                                <label class="control-label">关键字1</label>
	                                <div class="controls">
	                                	<input type="text" name="key" value="" style="width:200px" />相似关键字请用逗号分隔
	                                </div>
	                            </div>
	                            <div class="control-group" id="addKey-btn">
	                                <label class="control-label"></label>
	                                <div class="controls">
	                                    <a id="submitBtn" onclick="addKey()" class="btn btn-info btn-sm">添加关键字</a>不同关键字请添加
	                                </div>
	                            </div>
	                            <div class="control-group" id="lastDiv">
	                                <label class="control-label"></label>
	                                <div class="controls">
	                                    <a id="submitBtn" onclick="start()" class="btn btn-info btn-sm">开始</a>
	                                </div>
	                            </div>
                        		</#if>
                        	</form>
                        </div>
                    </div>
                    
                    <div class="table-content">
                    <#-- table -->
                        <table class="table-striped table-condensed table-hover" id="itemTable" style="width: 250px;max-width: 250px;margin-bottom: 20px;font-size:12px;word-break:break-all;word-wrap:break-word;">
                        </table>
                        <div class="pagination" id="itemPage"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
  <script>
    $(document).ready(function() {
    	<#if (status.drawTicketFlag || (!(status.endTime??)))  >time = setInterval("getResult()",1000);</#if>
	});
	var startFlag = true;
	var time;
	function start(){
		if (startFlag){
			startFlag = false;
			$("#problem").prop("readonly",true);
			var keys = $("input[name=key]");
			keys.prop("readonly",true);
			$.ajax({
				url : "${rc.contextPath}/admin/draw/ticket/start",
		    	dataType : "json",
		    	data :  $("#findForm").serialize(),
		    	type:'post',
		    	success:function(json){
		    		if (json.code == 0){
		    			if (json.data == "error"){
		    			} else {
		    				$("#addKey-btn").remove();
			    			var lastDiv = $("#lastDiv");
			    			var div = $('<div class="control-group"><label class="control-label">开始时间</label><div class="controls">'+json.data+'</div></div>')
			    			lastDiv.before(div);
			    			var btn = $("#submitBtn");
			    			btn.prop("disabled","disabled");
			    			btn.removeAttr("onclick");
			    			btn.html("竞猜中...");
			    			time = setInterval("getResult()",1000);
		    			}
		    		} else {
		    			alert("启动失败: "+json.message);
		    		}
		    		startFlag = true;
		    	}
			});
		}
	}
	var getResultFlag = true;
	function getResult(){
		if (getResultFlag){
			getResultFlag = false;
			$.ajax({
				url : "${rc.contextPath}/admin/draw/ticket/getResult",
		    	dataType : "json",
		    	type:'post',
		    	success:function(json){
		    		if (json.code == 0){
		    			if (json.data.auth != ""){
		    				location.reload();
		    			}
		    		} else {
		    			alert("获取失败: "+json.message);
		    		}
		    		getResultFlag = true;
		    	},
		    	error:function(){
		    		getResultFlag = true;
		    	}
			});
		}
	}
	
	function addKey(){
		var keys = $("input[name=key]");
		var index = keys.length+1;
		var newKye = $('<div class="control-group"><label class="control-label">关键字'+index+'</label><div class="controls"><input type="text" name="key" value=""style="width:200px" /></div></div>');
		$("#addKey-btn").before(newKye);
	}
	
	function clearStatus(){
		$.ajax({
			url : "${rc.contextPath}/admin/draw/ticket/clear",
	    	dataType : "json",
	    	type:'post',
	    	success:function(json){
	    		location.reload();
	    	}
		});
	}
	
		
   </script>
</body>
</html>